//
//  SYVisitView.swift
//  SYWeibo
//
//  Created by Saya on 16/4/7.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SnapKit

//如果想要使用optional的代理方法,且需要将协议也声明为objec的消息机制
@objc protocol SYVisitViewDelegate : NSObjectProtocol{
    optional func userWillRegister ()
    optional func userWillLogin ()
}

class SYVisitView: UIView {
    
    //提供一个属性
    weak var Delegate:SYVisitViewDelegate?
    
    //提供一个方法让外部可以通过设置参数的方式,设置界面的展示样式,所以不能是私有的
    func setUpVisitView (description :String,bigImgName:String,isHome:Bool = false) {
        if isHome {
            circleImgAnimation()
        }else{
            bigHouse.hidden = true
            bringSubviewToFront(crcleIcon)
        }
        titleLb.text = description
        crcleIcon.image = UIImage(named: bigImgName)
    }
    
    
    //转圈的图标
    private lazy var crcleIcon = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子图标
    private lazy var bigHouse = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //渐变的背景图片
    private lazy var feedBG = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //标题
    private lazy var titleLb:UILabel = {
        let tempLabel = UILabel()
        tempLabel.text = "关注一些人,回到这里看看有什么惊喜"
        tempLabel.font = UIFont.systemFontOfSize(14)
        tempLabel.textColor = UIColor.orangeColor()
        tempLabel.sizeToFit()
        tempLabel.numberOfLines = 0
        return tempLabel
    }()
    
    //设置注册按键
    private lazy var registerBtn :UIButton = {
        let tempRegisterBtn = UIButton()
//        tempRegisterBtn.bounds = CGRect(x: 0, y: 0, width: 100, height: 50)
        tempRegisterBtn.setTitle("注册", forState: .Normal)
        tempRegisterBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let tempImg = UIImage(named: "common_button_white_disable")
        
        tempRegisterBtn.setBackgroundImage(tempImg?.stretchableImageWithLeftCapWidth(Int(tempImg!.size.width*0.5), topCapHeight: Int(tempImg!.size.height*0.5)),forState: .Normal)
        //添加点击事件
        tempRegisterBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        return tempRegisterBtn
    }()
    
    //设置登录按键
    private lazy var loginBtn :UIButton = {
        let temploginBtn = UIButton()
        //        tempRegisterBtn.bounds = CGRect(x: 0, y: 0, width: 100, height: 50)
        temploginBtn.setTitle("登录", forState: .Normal)
        temploginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        temploginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //添加点击事件
        temploginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        return temploginBtn
    }()
    
    @objc private func registerBtnDidClick (){
        print("registerBtnDidClick")
        //使用代理去传值,在使用optional的代理方法,则需要判断该代理方法是否实现了
        Delegate?.userWillRegister?()
    }
    
    @objc private func loginBtnDidClick (){
        print("loginBtnDidClick")
        //使用代理,在使用optional的代理方法,则需要判断该代理方法是否实现了
        Delegate?.userWillLogin?()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //可以在出事化的方法中,将界面的不变的属性都添加好,在layoutSubView方法中设置子控件的位置,在数据模型的didset方法中设置view的赋值(可变的)
        plusSubView()
//        circleImgAnimation()
        
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func plusSubView (){
        addSubview(crcleIcon)
        //在大房子之前加载,这样背景图片就会有渐变的感觉
        addSubview(feedBG)
        addSubview(bigHouse)
        addSubview(titleLb)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置自动布局,使用snapKit
        crcleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        feedBG.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(crcleIcon.snp_center)
        }
        
        bigHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(crcleIcon.snp_center)
        }
        
        //比较大房子 和 圆环图片的大小
        titleLb.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(crcleIcon.snp_bottom)
            make.centerX.equalTo(crcleIcon.snp_centerX)
            make.width.equalTo(240)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(titleLb.snp_left)
            make.top.equalTo(titleLb.snp_bottom).offset(16)
            make.width.equalTo(80)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(titleLb.snp_right)
            make.top.equalTo(titleLb.snp_bottom).offset(16)
            make.width.equalTo(80)
        }
    }
    
    //设置圆环图片的动画效果
    private func circleImgAnimation () {
        //开启一个普通动画
        let baseAnim :CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的相关属性
        baseAnim.toValue = 2*M_PI
        baseAnim.duration = 14
        baseAnim.repeatCount = MAXFLOAT
        baseAnim.removedOnCompletion = false
        //添加动画到layer层
        crcleIcon.layer.addAnimation(baseAnim, forKey: nil)
    }
}


































